<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="唯一编号" prop="bh">
        <el-input
          v-model="queryParams.bh"
          placeholder="请输入唯一编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
       
       
      <el-form-item label="道路类型" prop="dllx">
        <el-select
          v-model="queryParams.dllx"
          placeholder="请选择道路类型"
          clearable
          class="!w-240px"
        >
         
        <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ROAD_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="道路名" prop="dlmc">
        <el-input
          v-model="queryParams.dlmc"
          placeholder="请输入道路名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
        <!--<el-select
          v-model="queryParams.dlmc"
          placeholder="请选择道路名称"
          clearable
          class="!w-240px"
        >
        <el-option
          v-for="dict in jclk"
            :key="dict.id"
            :label="dict.name"
            :value="dict.name"
          />
        </el-select>-->
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      
        
        <el-button
              style="height: 40px;float: right;"
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['intersection:intersection:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> 添加道路
            </el-button>
            <el-button
              style="height: 40px;"
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['intersection:intersection:export']"
            >
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button>
        
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>  
    
    <el-alert
    :closable="false"
    title="操作指引："
    type="success"> 
    先在本页面添加道路(当要增加杆件发现该杆件所在的道路不存在时，进入  <el-link type="primary" href="/device/jclk"  >道路管理</el-link>
    先增加该道路的基础数据，再到本页面添加。)、
    然后添加该道路下的杆件、最后添加杆件下的设备。<BR/>  <BR/>  
    道路（路口）、杆件、设备的关系如下：<br/>
    一条道路或路口有1-N个杆件（立杆），每个杆件（立杆）有1-N个附属的设备（如电警、全景球机等）。<BR/>
   
    1、当一条道路或者一个交叉路口有多个杆件，需在该道路或路口依次添加多个对应的杆件。<br/>
    2、当一个杆件有多个设备，需在该杆件下一次添加多个设备（如全景球机、电警等）。<br/></el-alert>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- 子表的列表 -->
      <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="barinfo" type="border-card">
            <el-tab-pane label="道路(路口)下属杆件列表" name="barinfo" >
              <barinfoList :dlbh="scope.row.bh" />
            </el-tab-pane>
            <!--<el-tab-pane label="杆件设备列表" name="deviceinfo">
              <deviceinfoList :lkbh="scope.row.bh" />
            </el-tab-pane>-->
          </el-tabs>
        </template>
      </el-table-column>
      <!--<el-table-column label="id" align="center" prop="id" width="50px"/>-->
      <el-table-column label="道路编号" align="center" prop="bh"  width="100px"/>
      <el-table-column label="道路类型" align="center" prop="dllx">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ROAD_TYPE" :value="scope.row.dllx" />
        </template>
      </el-table-column>
      <el-table-column label="杆件及设备所在道路（口）名称" align="center" prop="dlmc"  width="300px">
        <!--<template #default="scope">
          <dict-tag :type="DICT_TYPE.ROAD_TYPE" :value="scope.row.dlmc" />
        </template> -->
      </el-table-column>
      <el-table-column label="纬度" align="center" prop="lng"  width="140px"/>
      <el-table-column label="经度" align="center" prop="lat" />
      
     
      
      <!--<el-table-column label="对应的POI ID" align="center" prop="poiid" />-->
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['intersection:intersection:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['intersection:intersection:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <intersectionForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { intersectionApi, intersectionVO } from '@/api/intersection/intersection'
import intersectionForm from './intersectionForm.vue'
import barinfoList from './components/barinfoList.vue'
import deviceinfoList from './components/deviceinfoList.vue'

/** 交叉路口 列表 */
defineOptions({ name: 'Intersection' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<intersectionVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  bh: undefined,
  lng: undefined,
  lat: undefined,
  poiid: undefined,
  dllx: undefined,
  dlmc: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

//获取交叉路口列表
const jclk = ref<Record<string, any>[]>([]);
const getJclk= async()=>{
  const list = await intersectionApi.getjclkPage({  pageNo: 1, pageSize: 10000});
  jclk.value = list.list;
}
getJclk()


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await intersectionApi.getintersectionPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await intersectionApi.deleteintersection(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await intersectionApi.exportintersection(queryParams)
    download.excel(data, '交叉路口列表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>